$(function() {
	//设置活动按钮
	changeHeaderMenu(2);
	changeMenu(2, 0);
	savehandle();
	initTable();
	getCity();
});
function getCity(){
	$.getJSON('/fw/pages/agent/js/citys.json').done(function(data){
		var data = data.result.rows;
		var elementById = document.getElementById("province");
		for ( var int = 0; int < data.length; int++) {
			var d = data[int];
			var province = d.mainCity;
			var op=document.createElement("option");      
			op.setAttribute("class","province_o");        
			op.setAttribute("value",province);         
			op.appendChild(document.createTextNode(province));
			elementById.appendChild(op);         
		}
	});
}
function provinceChange(){
	var province = $('.province').val();
	$.getJSON('/fw/pages/agent/js/citys.json').done(function(data){
		var data = data.result.rows;
		var elementById = document.getElementById("city");
		elementById.options.length=0;
		for ( var int = 0; int < data.length; int++) {
			var d = data[int];
			var provinces = d.mainCity;
			if(province == provinces){
				var city = d.citys;
				for ( var i = 0; i < city.length; i++) {
					var op=document.createElement("option");      
					op.setAttribute("class","city_o");        
					op.setAttribute("value",city[i].name);         
					op.appendChild(document.createTextNode(city[i].name));
					elementById.appendChild(op);         
				}
				break;
			}
		}
	});
}

function initTable(){
	$.getJSON('/fw/agent/agentReg_findById.action', function(data) {
		data = data.result.rows[0]
		$('#agentName').val(data.agentName);
		$('#agentPhone1').val(data.agentPhone1);
		$('#agentPhone2').val(data.agentPhone2);
		$('#agentAddr').val(data.agentAddr);
		var agentAddr = data.agentAddr;
		var addr= new Array(); 
		addr=agentAddr.split("-"); 
		if(addr.length <= 1){
			$('#addr').val(addr[0]);
		}else {
		$('#province option:selected').html(addr[0]);
		var province = $('.province').val();
		$.getJSON('/fw/pages/agent/js/citys.json').done(function(data){
			var data = data.result.rows;
			var elementById = document.getElementById("city");
			elementById.options.length=0;
			for ( var int = 0; int < data.length; int++) {
				var d = data[int];
				var provinces = d.mainCity;
				if(province == provinces){
					var city = d.citys;
					var op=document.createElement("option");      
					op.setAttribute("class","city_o");        
					op.setAttribute("value",addr[1]);         
					op.appendChild(document.createTextNode(addr[1]));
					elementById.appendChild(op);         
					for ( var i = 0; i < city.length; i++) {
						var op=document.createElement("option");      
						op.setAttribute("class","city_o");        
						op.setAttribute("value",city[i].name);         
						op.appendChild(document.createTextNode(city[i].name));
						elementById.appendChild(op);         
					}
					break;
				}
			}
		});
		}
		$('#addr').val(addr[2]);
	});
	
}


function successCallBack() {
	window.location.href = '/fw/pages/home/login.jsp';
}

/**
 * 提示信息处理
 */
function savehandle() {
	$('#alertdiv').hide();
	$('#alertdiv').savehandle( {
		fieldMap : {},
		successMsg : '添加成功！',
		failMsg : '添加失败！错误：',
		success : successCallBack
	});
}
function ret(){
	$('#agentName').val("");
	$('#agentPhone1').val("");
	$('#agentPhone2').val("");
	$('#agentAddr').val("");
}

function change() {
	var agentName = $('#agentName').val();
	if (!agentName) {
		$('#alertdiv').savehandle('setmsg', {msg : '请输入名称!',timer : 2000});
		return;
	}
	var agentPhone1 = $('#agentPhone1').val();
	if(!agentPhone1){
		agentPhone1 = "";
	}
	var agentPhone2 = $('#agentPhone2').val();
	if(!agentPhone2){
		agentPhone2 = "";
	}
	var addr = $('#addr').val();
	var province = $('#province option:selected').html();
	var city = $('#city option:selected').html();
	if(city.indexOf('请选择') == -1)
		addr = city +"-"+ addr;
	if(province.indexOf('请选择') == -1)
		addr = province + "-" + addr;
	if(!addr){
		addr = "";
	}
	var data = {"agentName":agentName,"agentPhone1":agentPhone1,
			    "agentPhone2":agentPhone2,"agentAddr":addr};
	$.getJSON('/fw/agent/agentReg_changeAgent.action', data, function(data) {
		if (data.success) {
			$('#alertdiv').savehandle('setmsg', {
				msg : '成功!',
				timer : 2000
			});
			//window.location.href = '/fw/pages/agent/inCafeList.jsp';
		} else {
			var msg = data.result;
			$('#alertdiv').savehandle('setmsg', {
				msg : msg.errors,
				timer : 2000
			});
		}
	});
}
